package com.aircms.service.impl;

import com.aircms.common.ServerResponse;
import com.aircms.dao.PermissionMapper;
import com.aircms.dao.RolePermissionMapper;
import com.aircms.pojo.Permission;
import com.aircms.pojo.validation.BaseQueryFieldsValidation;
import com.aircms.pojo.vo.RolePermissionVo;
import com.aircms.service.IPermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author air
 * @create 2018-06-28 下午3:33
 */

@Service("iPermissionService")
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private Permission permission;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;


    public ServerResponse index(){
        List<Permission> permissions = permissionMapper.selectALl();
        if(permissions.isEmpty()){
            return ServerResponse.response("",-900);
        }
        return ServerResponse.response(permissions);
    }


    /**
     * 添加权限，如果已近存在则忽略（不做更新操作），否则添加
     * 只是同步路由
     * @param url
     * @param method
     * @return
     */
    public ServerResponse store(String url, String method){
        if(permissionMapper.selectByUrlAndMethod(url,method)>0){// 如果已经存在，则忽略
            return ServerResponse.response();
        }else {
            permission.setUrl(url);
            permission.setMethod(method);
            permission.setType((byte)1);                  // 路由权限
            permission.setStatus((byte)1);                // 路由权限
            permissionMapper.insertSelective(permission);
        }
        return ServerResponse.response();
    }


    public ServerResponse update(Permission permission){
        if(permissionMapper.updateByPrimaryKeySelective(permission)<1){
            return ServerResponse.response("",-2,"id 不存在");
        }
        return ServerResponse.response();
    }

    public ServerResponse destroy(Integer id){
        if(permissionMapper.deleteByPrimaryKey(id)<1){
            return ServerResponse.response("",-2,"id 不存在");
        }
        return ServerResponse.response();
    }


    public ServerResponse insertMenuPermission(Permission p){
        if(permissionMapper.selectMenuPermissionByName(p.getName())>0){// 不允许菜单名称重复
            return  ServerResponse.response("",-2,"name 已存在");
        }else {
            permission.setType((byte)2);
            permission.setName(p.getName());
            permission.setRemark(p.getRemark());
            permissionMapper.insertSelective(permission);
        }
        return ServerResponse.response();
    }


    public ServerResponse getMenuPermission(BaseQueryFieldsValidation baseQueryFields, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Permission> permissions=permissionMapper.selectMenuIndex(baseQueryFields);
        PageInfo pageInfo=new PageInfo(permissions);
        return ServerResponse.response(pageInfo);
    }


    /**
     * 路由权限，提供查询参数
     * @param baseQueryFields
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ServerResponse getRouterPermission(BaseQueryFieldsValidation baseQueryFields, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Permission> permissions=permissionMapper.selectRouterIndex(baseQueryFields);
        PageInfo pageInfo=new PageInfo(permissions);
        return ServerResponse.response(pageInfo);
    }



    /**
     * 获取所有路由
     * @return
     */
    public ServerResponse getAllRoutes(Integer roleId){
        List<Permission> permissions=permissionMapper.selectPermissionByType(1);

        String[] string=new String[1];
        string[0]= String.valueOf(roleId);
        List<RolePermissionVo>permissionVo=rolePermissionMapper.getRolePermissionIdsByRoleIs(string,1);
        permissions.forEach(item->{
            permissionVo.forEach(item1->{
                if(item.getId().equals(item1.getPermissionId())){
                    item.set_checked(true);
                }
            });
        });

        return ServerResponse.response(permissions);
    }

    /**
     * 获取所有路由
     * @return
     */
    public ServerResponse getAllRoutes(){
        List<Permission> permissions=permissionMapper.selectPermissionByType(1);
        return ServerResponse.response(permissions);
    }



    /**
     * 获取所有菜单
     * @return
     */
    public ServerResponse getAllMenu(Integer roleId){
        List<Permission> permissions=permissionMapper.selectPermissionByType(2);
        String[] string=new String[1];
        string[0]= String.valueOf(roleId);
        List<RolePermissionVo>permissionVo=rolePermissionMapper.getRolePermissionIdsByRoleIs(string,2);
        permissions.forEach(item->{
            permissionVo.forEach(item1->{
                if(item.getId().equals(item1.getPermissionId())){
                    item.set_checked(true);
                }
            });
        });
        return ServerResponse.response(permissions);
    }


}
